package com.boss.salary.dao.impl;

import com.boss.framework.dao.BaseDaoImpl;
import com.boss.framework.dto.PaginationDTO;
import com.boss.salary.dao.IPersonSalaryInfoDao;
import com.boss.salary.entity.PersonSalaryInfo;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class PersonSalaryInfoDaoImpl extends BaseDaoImpl implements IPersonSalaryInfoDao {

    private final static String TABLE_NAME = "person_salary_info";

    @Override
    public List<PersonSalaryInfo> queryPersonSalaryInfoPage(PaginationDTO paginationDTO) {
        StringBuilder sql = new StringBuilder();
        sql.append("select id, biz_key, create_time, update_time, agency_id, person_id,")
                .append("salary_date, pay_state, verify_state, is_thirteen, is_deleted")
                .append(" from ").append(TABLE_NAME);
        sql.append(" order by create_time");
        String querySql = this.boxUpSql(sql.toString());
        //请求参数初始化
        Object[] params = this.boxUpParam(null, paginationDTO);
        return this.getJdbcTemplate().query(querySql, new BeanPropertyRowMapper(PersonSalaryInfo.class), params);
    }

    @Override
    public int queryPersonSalaryInfoTotal() {
        StringBuilder sql = new StringBuilder();
        sql.append("select count(*) from ").append(TABLE_NAME);
        return this.getJdbcTemplate().queryForObject(sql.toString(), Integer.class);
    }

    @Override
    public void savePersonSalaryInfo(PersonSalaryInfo personSalaryInfo) {
        StringBuilder sql = new StringBuilder();
        sql.append("insert into ").append(TABLE_NAME);
        sql.append("(id, biz_key, create_time, update_time, agency_id, person_id, salary_date,")
                .append("pay_state, verify_state, is_thirteen, is_deleted )");
        sql.append("values (?,?,?,?,?,?,?,?,?,?,?)");
        Object[] params = new Object[]{
                personSalaryInfo.getId(), personSalaryInfo.getBizKey(), personSalaryInfo.getCreateTime(),
                personSalaryInfo.getUpdateTime(), personSalaryInfo.getAgencyId(), personSalaryInfo.getPersonId(),
                personSalaryInfo.getSalaryDate(), personSalaryInfo.getPayState(), personSalaryInfo.getVerifyState(),
                personSalaryInfo.getIsThirteen(), personSalaryInfo.getIsDeleted()
        };
        this.getJdbcTemplate().update(sql.toString(), params);
    }

    @Override
    public void updatePersonSalaryInfo(PersonSalaryInfo personSalaryInfo) {
        StringBuilder sql = new StringBuilder();
        sql.append("update ").append(TABLE_NAME);
        sql.append(" set biz_key=?, update_time=?, agency_id=?, person_id=?, salary_date=?,")
                .append("pay_state=?, verify_state=?, is_thirteen=?, is_deleted=? where id=?");
        Object[] params = new Object[]{
                personSalaryInfo.getBizKey(),
                personSalaryInfo.getUpdateTime(), personSalaryInfo.getAgencyId(), personSalaryInfo.getPersonId(),
                personSalaryInfo.getSalaryDate(), personSalaryInfo.getPayState(), personSalaryInfo.getVerifyState(),
                personSalaryInfo.getIsThirteen(), personSalaryInfo.getIsDeleted(), personSalaryInfo.getId()
        };
        this.getJdbcTemplate().update(sql.toString(), params);
    }

    @Override
    public void deletePersonSalaryInfo(String id) {
        StringBuilder sql = new StringBuilder();
        sql.append("update ").append(TABLE_NAME);
        sql.append(" set is_deleted='1' ")
                .append("where id=?");
        Object[] params = new Object[]{
                id
        };
        this.getJdbcTemplate().update(sql.toString(), params);
    }
}
